﻿using ASmile.Com;
using ASmile.DBModel;
using ASmile.Helpers;
using ASmile.Win.Controls;
using ASmile.Win.Extend;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmGridColumn_bak : FrmCrud
    {
        public FrmGridColumn_bak()
        {
            InitializeComponent();
        }

        readonly IGridViewBLL BLLObj = ObjectFactory.GetObject<IGridViewBLL>("GridViewBLL");

        ArgPageData SearchArg { get; } = new ArgPageData();

        #region CRUD 重写

        /// <summary>
        /// 创建新增窗体的方法
        /// </summary>
        protected override FrmBaseEdit CreateAddForm()
        {
            var frm = new FrmGridColumnEdit();
            frm.OwnerForm = Owner;
            frm.SearchFormName = SearchArg.TagData.ToString(); //BLLObj.SearchForm;
            frm.DgvRowCount = dgv.RowCount;
            return frm;
        }
        /// <summary>
        /// 获取数据源的方法 （必须重写）
        /// </summary>
        protected override object GetDataSource(int pageNo, int pageSize, string searchVal, out int rowCount)
        {
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.SearchVal = searchVal;

            //var r = BLLObj.GetData(SearchArg);
            //rowCount = r.count;
            //return r.data;
            rowCount = 0;
            return null;
        }
        /// <summary>
        /// 新增处理
        /// </summary>
        protected override ApiResult<string> AddSaveHandle(object m)
        {
            return BLLObj.Add(m as Sys_GridColumn);
        }
        /// <summary>
        /// 更新处理
        /// </summary>
        protected override ApiResult<string> EditSaveHandle(object m)
        {
            //return BLLObj.Edit(m as Sys_GridColumn);
            return null;
        }

        public override object GetGridSelected()
        {
            return dgv.GetSelectedClassData<Sys_GridColumn>();
        }

        /// <summary>
        /// 删除处理
        /// </summary>
        protected override ApiResult DelHandle(object m)
        {
            var ret = BLLObj.Del(m as Sys_GridColumn);
            return ret;
        }
        /// <summary>
        /// 创建保存实体对象
        /// </summary>
        public override object CreateSaveModel()
        {
            return new Sys_GridColumn();
        }

        protected override void AdSearch_Click(object sender, EventArgs e)
        {
            ADSearch(SearchArg, BindDataSource);
        }

        protected override void BtnSearch_Click(object sender, EventArgs e)
        {
            SearchArg.ClearADList();
            base.BtnSearch_Click(sender, e);
        }

        #endregion

        private void FrmGridColumn_Load(object sender, EventArgs e)
        {
            if (Owner is IFormContainer dgvContainer)
            {
                //BLLObj.SearchForm = dgvContainer.ContainerName;
                SearchArg.TagData = dgvContainer.ContainerName;
                BtnSearch_Click(sender, e);
            }
            if (IsManager)
            {
                toolStripTop.LoopItem(SetRights);
                void SetRights(ToolStripItem item)
                {
                    if (!item.Enabled) item.Enabled = item.Visible = true;
                }
            }
            dgv.SetInputEdit<Sys_GridColumn>(Sys_GridColumn.CNColumnIndex, txtColumnIndex, SetCellEdit);
            dgv.SetDropMove<Sys_GridColumn>(SwapOrderNo);
            SetCopyInsertSql<Sys_GridColumn>(dgv, Keys.P);
        }

        bool SetCellEdit(Sys_GridColumn m, EditText txt)
        {
            if (m.ColumnIndex == txtColumnIndex.Value)
            {
                return false;
            }
            var ret = BLLObj.SetColumnIndex(new ArgSetSortNo { Id = m.Id,SortNo = (int)txtColumnIndex.Value}); //m.Id, (int)txtColumnIndex.Value);
            ShowMessage(ret.msg);
            return true;
        }

        void SwapOrderNo(Sys_GridColumn srcModel, Sys_GridColumn tarModel)
        {
            ArgSwapOrderNo arg = new ArgSwapOrderNo
            {
                SrcId = srcModel.Id,
                SrcOrderNo = srcModel.ColumnIndex,
                TarId = tarModel.Id,
                TarOrderNo = tarModel.ColumnIndex
            };
            var ret = BLLObj.SwapOrderNo(arg);
            if (ret)
            {
                tarModel.ColumnIndex += srcModel.ColumnIndex;
                srcModel.ColumnIndex = tarModel.ColumnIndex - srcModel.ColumnIndex;
                tarModel.ColumnIndex = tarModel.ColumnIndex - srcModel.ColumnIndex;
                dgv.Refresh();
            }
            ShowMessage(ret.msg);
        }

        private void tsmiCopyInsertSQL_Click(object sender, EventArgs e)
        {
            var rList = dgv.DataSource as List<Sys_GridColumn>;
            var sql = SqlScriptTool.GetInsertSQL(rList);
            Clipboard.SetText(sql.ToString());
            MessageBox.Show("sql 脚本已经赋值到剪切板！");
        }
    }
}